<template>
  <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" label-width="auto" class="demo-ruleForm"
    status-icon>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>
    <el-form-item label="密码" prop="userpass">
      <el-input v-model="ruleForm.userpass" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button @click="denglu">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { useUserStore } from '@/stores/user';
import axios from 'axios';
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter()
const store = useUserStore()
const ruleForm = reactive({
  userName: "",
  userpass: ""
})


const denglu = () => {
  axios.get("http://localhost:18675/api/RbacReadAll/GetLogin", {
    params: {
      userName: ruleForm.userName,
      userpass: ruleForm.userpass,
    }
  })
    .then(res => {
      if (res.data.code == 200) {
        store.userinfo.uId = res.data.data.uId
        store.userinfo.userName = res.data.data.userName
        store.userinfo.userNickname = res.data.data.userNickname
        store.userinfo.userpass = res.data.data.userpass
        store.userinfo.userNickname = res.data.data.userNickname
        store.userinfo.token = res.data.data.token
        console.log(store.userinfo.token);
        // 登录判定
        ElMessage.success("登录成功")
        // 登录成功，写JWT的 token到本地
        window.localStorage.setItem("token", res.data.token);
        // 数据提交后端要加入新的Token值
        axios.defaults.headers.common["Authorization"] =
          "Bearer " + res.data.token;

        router.push("/MainView")
      } else {
        ElMessage.error("登录失败")
      }
    })
}

</script>

<style></style>